package net.sx.minemodule.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.sx.minemodule.R;

/**
 * 优惠券的tabView
 * @author 11470
 */
public class MineDiscountTabView extends LinearLayout {

    private RelativeLayout rlnOneTab;
    private RelativeLayout rlnTwoTab;
    private TextView tvOneTab;
    private TextView tvTwoTab;
    private View viewOneTab;
    private View viewTwoTab;

    private int selectedColorId = R.color.color_a541c9;
    private int noSelectColorId = R.color.color_333333;

    private MineOrderTabView.OneTabClickListener oneTabClickListener;
    private MineOrderTabView.TwoTabClickListener twoTabClickListener;

    public MineDiscountTabView(Context context) {
        this(context,null);
    }

    public MineDiscountTabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MineDiscountTabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
       View view = LayoutInflater.from(context).inflate(R.layout.mine_view_discount_tab, this, true);
       rlnOneTab = view.findViewById(R.id.rln_one_tab);
       rlnTwoTab = view.findViewById(R.id.rln_two_tab);
       tvOneTab = view.findViewById(R.id.tv_one_tab);
       tvTwoTab = view.findViewById(R.id.tv_two_tab);
       viewOneTab = view.findViewById(R.id.view_one_tab);
       viewTwoTab = view.findViewById(R.id.view_two_tab);

        tvOneTab.setTextColor(getResources().getColor(selectedColorId));
        viewOneTab.setBackgroundColor(getResources().getColor(selectedColorId));
        tvTwoTab.setTextColor(getResources().getColor(noSelectColorId));
        viewTwoTab.setBackgroundColor(getResources().getColor(noSelectColorId));
        initEvent();
    }

    private void initEvent() {
        rlnOneTab.setOnClickListener(v -> {
            tvOneTab.setTextColor(getResources().getColor(selectedColorId));
            viewOneTab.setBackgroundColor(getResources().getColor(selectedColorId));
            viewOneTab.setVisibility(VISIBLE);
            tvTwoTab.setTextColor(getResources().getColor(noSelectColorId));
            viewTwoTab.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewTwoTab.setVisibility(INVISIBLE);
            if (oneTabClickListener!=null){
                oneTabClickListener.onOneTabClick(v);
            }
        });

        rlnTwoTab.setOnClickListener(v -> {
            tvOneTab.setTextColor(getResources().getColor(noSelectColorId));
            viewOneTab.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewOneTab.setVisibility(INVISIBLE);
            tvTwoTab.setTextColor(getResources().getColor(selectedColorId));
            viewTwoTab.setBackgroundColor(getResources().getColor(selectedColorId));
            viewTwoTab.setVisibility(VISIBLE);
            if (twoTabClickListener!=null){
                twoTabClickListener.onTwoTabClick(v);
            }
        });

    }

    public void setOneTabClickListener(MineOrderTabView.OneTabClickListener oneTabClickListener) {
        this.oneTabClickListener = oneTabClickListener;
    }

    public void setTwoTabClickListener(MineOrderTabView.TwoTabClickListener twoTabClickListener) {
        this.twoTabClickListener = twoTabClickListener;
    }

    public interface OneTabClickListener{
        void onOneTabClick(View view);
    }

    public interface TwoTabClickListener{
        void onTwoTabClick(View view);
    }

}
